package cn.group.service.impl;

import cn.group.mapper.RoleManagerMapper;
import cn.group.service.RoleManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.entity.Role;
import pojo.entity.RolePermission;
import pojo.qo.RolePermissionQO;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleManagerServiceImpl implements RoleManagerService {

    @Autowired
    private RoleManagerMapper roleManagerMapper;

    /**
     * 添加角色、权限信息
     * @param rolePermissionQO
     * @return
     */
    @Override
    public int addRole(RolePermissionQO rolePermissionQO) {
        //保存角色信息
        String roleName = rolePermissionQO.getRoleName();
        String description = rolePermissionQO.getDescription();
        Role role = new Role();
        role.setRoleName(roleName);
        role.setDescription(description);
        addRoleMessage(role);
        //获取角色ID
        Role role1 = showRole();
        Long id = role1.getId();
        //批量插入角色权限关联列表
        List<RolePermission> list = getPermissionList(rolePermissionQO, id);
        addRolePermission(list);
        return 1;
    }

    public int addRoleMessage(Role role) {
        return roleManagerMapper.addRoleMessage(role);
    }

    public int addRolePermission(List<RolePermission> listRolePermission) {
        return roleManagerMapper.addRolePermission(listRolePermission);
    }

    public Role showRole() {
        return roleManagerMapper.showRole();
    }
    /**
     * 查看角色、权限信息
     * @param id
     * @return
     */
    @Override
    public RolePermissionQO showRolePermissionById(Long id) {
        //通过角色id拿到权限Id
        Long[] list = PermissionList(id);
        //通过角色id拿到权限信息
        Role role = showRoleById(id);
        String roleName = role.getRoleName();
        String description = role.getDescription();
        //给 RolePermissionQO 赋值
        RolePermissionQO rolePermissionQO = new RolePermissionQO();
        rolePermissionQO.setRoleName(roleName);
        rolePermissionQO.setDescription(description);
        rolePermissionQO.setPermissionId(list);
        return rolePermissionQO;
    }

    /**权限id列表
     *
     * @param id
     * @return 返回一个权限数组
     */
    private Long[] PermissionList(Long id) {
        List<RolePermission> permissionList = showPermissionById(id);
        RolePermission rolePermission;
        int size = permissionList.size();
        Long[] list = new Long[size];
        for (int i = 0; i < size; i++) {
            rolePermission = permissionList.get(i);
            Long permissionId = rolePermission.getPermissionId();
            list[i] = permissionId;
        }
        return list;
    }

    public List<RolePermission> showPermissionById(Long id){
        return roleManagerMapper.showRoleByIdPermission(id);
    }

    @Override
    public Long[] ff(Long roleId) {
        return roleManagerMapper.ff(roleId);
    }

    /**
     * 修改角色权限信息
     *
     * @param rolePermissionQO
     * @return
     */
    @Override
    public int updateRolePermissionById(RolePermissionQO rolePermissionQO) {
        //修改角色信息对象
        Long roleId = rolePermissionQO.getRoleId();
        System.out.println("roleId: " + roleId);
        Role role = showRoleById(roleId);
        role.setRoleName(rolePermissionQO.getRoleName());
        role.setDescription(rolePermissionQO.getDescription());
        updateRole(role);
        //修改权限信息对象
        //删除该角色所有的权限
        roleManagerMapper.deletePermission(roleId);
        //批量插入角色权限关联列表
        List<RolePermission> list = getPermissionList(rolePermissionQO, roleId);
        addRolePermission(list);
        return 1;
    }

    //对权限id批量赋值
    private List<RolePermission> getPermissionList(RolePermissionQO rolePermissionQO, Long roleId) {
        Long[] permissionId = rolePermissionQO.getPermissionId();
        List<RolePermission> list = new ArrayList();
        for (Long perId : permissionId) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(perId);
            list.add(rolePermission);
        }
        return list;
    }

    @Override
    public int updateRole(Role role) {
        return roleManagerMapper.updateRole(role);
    }

    @Override
    public int deleteRole(Long roleId) {
        return roleManagerMapper.deleteRole(roleId);
    }

    @Override
    public List showRoleList() {
        return roleManagerMapper.showRoleList();
    }

    @Override
    public Role showRoleById(Long id) {
        return roleManagerMapper.showRoleById(id);
    }

}
